package net.zjitc.servlet.product;

import net.zjitc.dao.product.impl.ProductDaoImpl;
import net.zjitc.domain.Product;
import net.zjitc.service.impl.ProductServiceImpl;
import net.zjitc.service.product.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by hasee on 2017/10/24.
 */
@WebServlet(name = "ProductListByPageServlet",urlPatterns = "/productListByPageServlet")
public class ProductListByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String page = request.getParameter("page");
        if(page==null||"".equals(page)){
            response.sendRedirect("/manger/productListByPageServlet?page=1");
        }else {
            ProductService service = new ProductServiceImpl();
            List list = service.searchByPage(Integer.valueOf(page));
            request.setAttribute("productlist",list);
            List all = service.findAll();
            int size = (int) Math.ceil(Float.valueOf(all.size())/ProductDaoImpl.PAGE_SIZE);
            request.setAttribute("size",size);
            request.getRequestDispatcher("/product/productlist2.jsp").forward(request,response);
        }
    }
}
